import { createStore } from 'vuex'

export default createStore({
  state () {
    const token = localStorage.getItem('token') ?? null
    return {
      token
    }
  },
  mutations: {
    saveToken (state, token) {
      if (token) {
        state.token = token
        localStorage.setItem('token', token)
      } else {
        state.token = null
        localStorage.removeItem('token')
      }
    }
  },
  actions: {
    didLogin ({ commit }, token) {
      commit('saveToken', token)
    },
    logout ({ commit }) {
      commit('saveToken', null)
    }
  },
  modules: {
  }
})
